package com.appshelves.utils;

import java.util.List;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

public class ComponentHelper {

    public static String makeComponentNameString(ResolveInfo info) {
        final String packageName = packageNameByResolbeInfo(info);
        return new ComponentName(packageName, info.activityInfo.name).flattenToShortString();
    }

    public static boolean isPackageSelf(ResolveInfo info) {
        return getSelfPackageName().equalsIgnoreCase(packageNameByResolbeInfo(info));
    }

    public static boolean isPackageSelf(String packageName) {
        return getSelfPackageName().equalsIgnoreCase(packageName);
    }

    public static ResolveInfo resolveInfoByComponentName(String componentNameString) {
        return getPackageManager().resolveActivity(prepareBasicIntent(componentNameString),
            PackageManager.MATCH_DEFAULT_ONLY);
    }

    public static ResolveInfo resolveInfoByPackageName(String packageName) {
        Intent mainIntent = makeLauncherIntent();
        mainIntent.setPackage(packageName);

        List<ResolveInfo> infos = getPackageManager().queryIntentActivities(mainIntent, 0);
        if (infos == null || infos.size() == 0) // widget or addon package
            return null;
        return infos.get(0);
    }

    private static String packageNameByResolbeInfo(ResolveInfo info) {
        return info.activityInfo.applicationInfo.packageName;
    }

    public static String packageNameByComponentName(String componentNameString) {
        return packageNameByResolbeInfo(resolveInfoByComponentName(componentNameString));
    }

    private static String getSelfPackageName() {
        return Utils.getContext().getPackageName();
    }

    ///

    public static boolean isSystemApp(String componentNameString) {
        return (resolveInfoByComponentName(componentNameString).activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
    }

    public static boolean isPackageDisabled(String packageName) {
        return resolveInfoByPackageName(packageName) == null;
    }

    /// intent

    public static Intent prepareIntent(String componentNameString) {
        Intent intent = prepareBasicIntent(componentNameString);
        setIntentFlags(intent);
        return intent;
    }

    static Intent prepareBasicIntent(String componentNameString) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(ComponentName.unflattenFromString(componentNameString));
        return intent;
    }

    private static void setIntentFlags(Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    }

    /// 

    private static Intent makeLauncherIntent() {
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        return mainIntent;
    }

    public static List<ResolveInfo> getRawApps() {
        return getPackageManager().queryIntentActivities(makeLauncherIntent(), 0);
    }

    public static PackageManager getPackageManager() {
        return Utils.getContext().getPackageManager();
    }

    ///

}
